#include "SDL/SDL.h"
#include <string>
#include "SDL/SDL_mixer.h"
#include "myaudio.h"

using namespace std;


// ------------------------- class MyAudio def ---------------------------------------
//------------------------------------------------------------------------------------
MyAudio::MyAudio()
{

    int audio_rate = 22050;
    Uint16 audio_format = AUDIO_S16; // 16-bit stereo
    int audio_channels = 2;
    int audio_buffers = 2048;

    if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers))
    {
        printf("Unable to open audio!\n");
        exit(1);
    }

    has_a_music_thread = false;

    clear_sounds = 0;

    //Mix_ChannelFinished( MyAudio::callback_start );

    main_music = new Sound_effect();
}

MyAudio::~MyAudio()
{
    Mix_HaltChannel(-1);

    clear_effect_data();

    delete main_music;

    Mix_CloseAudio();
}

void MyAudio::clear_effect_data( list< Sound_effect * > tmp )
{
    cout << "tmp_list: " << tmp.size() << endl;
    int index = tmp.size();
    for( int c = 0; c < index; c++ )
    {
        cout << "c: " << c << endl;
        Sound_effect *se_temp = tmp.back();

        delete se_temp;

        tmp.pop_back();
        cout << "removed item from temp_list\n";
    }

}

void MyAudio::clear_effect_data()
{
    cout << "sound_effect_list: " << sound_effect_list.size() << endl;
    int index = sound_effect_list.size();
    for( int c = 0; c < index; c++ )
    {
        cout << "c: " << c << endl;
        Sound_effect *tmp = sound_effect_list.back();

        delete tmp;

        sound_effect_list.pop_back();
        cout << "removed item from sound_effect_list\n";
    }

}

/* -- string option --
 * LF --loop forever
 * LO --loop once
 */
void MyAudio::create_audio_thread( string file_name, string option )
{
    static list< Sound_effect * > tmp0;

    if( 5 == sound_effect_list.size() )
    {
        tmp0 = sound_effect_list;
        sound_effect_list.clear();
        cout << "size\n";
    }
    else if( 0 == (clear_sounds % 7) )
    {
        clear_sounds = 0;
        cout << "clear sounds_count\n";
        clear_effect_data( tmp0 );
        tmp0.clear();
    }

    // load correct sound file
    if( 0 == option.compare( "LF" ) && false == has_a_music_thread )
    {
        main_music->load_sound( file_name );

        main_music->sound_effect_thread = SDL_CreateThread( main_music->start_loop_thread, main_music );
        has_a_music_thread = true;
    }
    else if( 0 == option.compare( "LO" ) )
    {
        Sound_effect *tmp = new Sound_effect();
        tmp->load_sound( file_name );

        tmp->sound_effect_thread = SDL_CreateThread( tmp->start_once_thread, tmp );

        //cout << "\nsound_effect_list size: " << sound_effect_list.size() << endl;
        sound_effect_list.push_front( tmp );
        //cout << "sound effect added to sound_effect_list\n";
        cout << "sound_effect_list size: " << sound_effect_list.size() << endl << endl;

        clear_sounds++;
    }
    else
    {
        cout << "wrong option or your trying to add another main music thread\n";
    }

    cout << "\nclear_sounds: " << clear_sounds << endl;

}






